package com.rs.action;

import com.opensymphony.xwork2.ActionSupport;
import com.rs.entities.Nearby;
import com.rs.entities.ResultBean;
import com.rs.entities.ScenicRequest;
import com.rs.service.TouristInfoService;

import net.sf.json.JSONObject;

public class TouristInfoAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private TouristInfoService touristInfoService;

	public void setTouristInfoService(TouristInfoService touristInfoService) {
		this.touristInfoService = touristInfoService;
	}

	private String data;// 客户端传来的数据

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	private JSONObject result;// 返回给客户端的结果

	public JSONObject getResult() {
		return result;
	}

	public void setResult(JSONObject result) {
		this.result = result;
	}

	public String getScenic() {
		System.out.println("getScenic:" + data);
		ScenicRequest scenicRequest = null;
		try {
			if (data != null) {
				scenicRequest = (ScenicRequest) JSONObject.toBean(JSONObject.fromObject(data), ScenicRequest.class);
			}
			result = touristInfoService.getScenicList(scenicRequest);
			result.put("code", 200);
		} catch (Exception e){
			e.printStackTrace();
			result = new JSONObject();
			result.put("code", 400);
		}
		return "getScenic";

	}

	public String getHotel() {
		System.out.println("getHotel:" + data);
		try {
			Nearby nearby = (Nearby) JSONObject.toBean(JSONObject.fromObject(data), Nearby.class);
			result = touristInfoService.getHotelList(nearby);
			result.put("code", 200);
		} catch (Exception e) {
			e.printStackTrace();
			result = new JSONObject();
			result.put("code", 400);
		}
		return "getHotel";
	}

	public String getRestaurant() {
		System.out.println("getRestaurant:" + data);
		try {
			Nearby nearby = (Nearby) JSONObject.toBean(JSONObject.fromObject(data), Nearby.class);
			result = touristInfoService.getRestaurantList(nearby);
			result.put("code", 200);
		} catch (Exception e) {
			e.printStackTrace();
			result = new JSONObject();
			result.put("code", 400);
		}
		return "getRestaurant";
	}

	public String insert() {
		System.out.println("receiveData:" + data);
		ResultBean resultBean = null;
		try {
			touristInfoService.insert(data);
			resultBean = new ResultBean(200, "success", data);
		} catch (Exception e) {
			e.printStackTrace();
			resultBean = new ResultBean(400, "failure", "服务器存入数据失败");
		}
		result = JSONObject.fromObject(resultBean);
		return "insert";
	}
}
